//+------------------------------------------------------------------+
//|                                                 SpreadTwoCcy.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright"
#property link "yeloparis@yahoo.co.uk" 
#property indicator_separate_window 
#property indicator_buffers 4
#property indicator_color1 Orange
#property indicator_color2 Red
#property indicator_color3 Red 
#property indicator_color4 Blue  
//#property indicator_color5 Red
//---- Currencies-------------------------------------------------------
extern string Synbol     = "EURUSD"; 
extern string SynbolCorr = "EURGBP";   
//---- indicator parameters---------------------------------------------
extern int    BandsPeriod=5;
extern int    BandsShift=0;
extern double BandsDeviations=3.000;   
extern int    MethodMA=1;// MODE_SMA 0 Simple moving average; MODE_EMA 1 
extern int    Method=0; //PRICE_CLOSE 0  
extern int    MA_Shift=0;
extern double Deviation=1.75;
static string   short_name;
//---- buffers Lines----------------------------------------------------
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
double MovingBufferMovingAverage[]; 
int ExtCountedBars=0; 
//-------------------------------------------------------------------------
int init()
   {
   string short_name;   
   /*RefreshRates();/*When expert advisor has been calculating for 
   a long time and needs data refreshing*/
   
   IndicatorBuffers(4);
   /*
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)); 
   */
   SetIndexStyle(0,DRAW_LINE); 
   SetIndexBuffer(0,MovingBuffer);
   SetIndexStyle(1,DRAW_LINE);  
   SetIndexBuffer(1,UpperBuffer);
   SetIndexStyle(2,DRAW_LINE); 
   SetIndexBuffer(2,LowerBuffer);
   
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,MovingBufferMovingAverage); 
   
   /*
   SetIndexDrawBegin(0,MovingBuffer);/*Sets the bar number (from the data
   beginning) from which the drawing of given indicator line must start)*/
   /*
   SetIndexDrawBegin(0,UpperBuffer);
   SetIndexDrawBegin(0,LowerBuffer); 
   SetIndexDrawBegin(0,MovingBufferMovingAverage);    
   */  
   short_name="Spread & BANDS ("+BandsPeriod+")";   
   IndicatorShortName(short_name);
   
   SetIndexLabel(0,short_name);/*Sets drawing line descrip for showing 
   in the DataWindow and in the tooltip.*/
   SetIndexLabel(1,"Upper Band"); 
   SetIndexLabel(2,"Lower Band"); 
   SetIndexLabel(3,"Moving Average");
   if(Deviation<0.1) Deviation=0.1;
   if(Deviation>100.0) Deviation=100.0;
  
   //----Will Show Info on Indicator-----------
   CreateInfoObjects();//added
   //------------------------------------------         
   return(0);
   }
//----------------------------------------------------------------------
int start() 
   {
//======Type your name==================================================
   /*
   ObjectCreate ("Email", OBJ_LABEL,0,0,0);              
   ObjectSet    ("Email", OBJPROP_CORNER,1);          
   ObjectSet    ("Email", OBJPROP_XDISTANCE,10);              
   ObjectSet    ("Email", OBJPROP_YDISTANCE,5);              
   ObjectSetText("Email", "Philippe DADJO",11, "Bookman Old Style", Yellow);
   
   ObjectCreate ("Nom", OBJ_LABEL,0,0,0);              
   ObjectSet    ("Nom", OBJPROP_CORNER,1);          
   ObjectSet    ("Nom", OBJPROP_XDISTANCE,10);              
   ObjectSet    ("Nom", OBJPROP_YDISTANCE,20);              
   ObjectSetText("Nom", "Ahou DADJO",11, "Bookman Old Style", Yellow);   
   */ 
   
   /*
   ObjectCreate ("Test",OBJ_LABEL,0,0,0); 
   ObjectSet    ("Test", OBJPROP_CORNER,4);   
   ObjectSet    ("Test",OBJPROP_XDISTANCE,3);
   ObjectSet    ("Test",OBJPROP_YDISTANCE,12); 
   ObjectSetText("Test","YOYO: "+DoubleToStr(MovingBufferMovingAverage,6),12,"Tahoma",Red);
   */
      /* 
      double currentMA;
      int j;
      currentMA = iMA(0,PERIOD_H1,20,0,MODE_SMA,PRICE_TYPICAL,j);
      ObjectCreate("maValue",OBJ_LABEL,0,Time[j],currentMA);
      ObjectSetText("maValue",currentMA,8,"Tahoma",Gold);
      
      currentMA = iMA(0,PERIOD_H1,20,0,MODE_SMA,PRICE_TYPICAL,i);
      ObjectCreate("maValue"+i,OBJ_LABEL,0,Time[i],currentMA+0.0005);
      ObjectSetText("maValue"+i,currentMA,8,"Tahoma",Gold);
      */
//======================================================================     
   int counted_bars=IndicatorCounted();
   int i;
   int limit; 
   
   
   double deviation;
   double sum,oldval,newres;
//---- check for possible errors
   if(counted_bars<0) return(-1); 
         
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
//---- main loop      
   for(i=0; i<limit; i++)     
   {    
   if (iClose(SynbolCorr,0,i) != 0)
   {
       MovingBuffer[i]=MathLog(iClose(Synbol,0,i)/iClose(SynbolCorr,0,i));    
   }
   
   //----Text about levels -------------------
   
   ObjectCreate("BarUp", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet("BarUp", OBJPROP_CORNER, 3);
   ObjectSet("BarUp", OBJPROP_XDISTANCE, 20);
   ObjectSet("BarUp", OBJPROP_YDISTANCE,30); 
   ObjectSetText("BarUp", "Philippe Dadjo = "+Symbol()+"  "+MovingBuffer[i], 10, "Arial Black", Green);      
     
   
   ObjectCreate("Bar", OBJ_LABEL, 1, 0, 0, 0, 0);
   ObjectSet("Bar", OBJPROP_CORNER, 3);
   ObjectSet("Bar", OBJPROP_XDISTANCE, 400); 
   ObjectSet("Bar", OBJPROP_YDISTANCE,30); 
   ObjectSetText("Bar", "Philippe Dadjo = "+Symbol()+"  "+MovingBuffer[i], 10, "Arial Black", Green);       
   
   
   
   double FreeMargin=AccountFreeMargin();
   Comment(""
   ,"\n",      "Account free margin: ",DoubleToStr(FreeMargin,2) 
   ,"\n",      ""
   ,"\n",      "Current time : ",TimeToStr(TimeCurrent()) 
   ,"\n",      ""
   ,"\n",      "Current time is of System     ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)
   ,"\n",      ""
   ,"\n",      "How are you Brother ",MovingBuffer[i] 
   ,"\n",      ""
   ,"\n",      "Current time is of System: ",TimeToStr(TimeCurrent(),TIME_SECONDS));
  
 
   /*
   datetime    T1,
               T2;
   double      P1,
               P2;
   P1 = Open[2];
   T1 = Time[2];
  
   P2 = Open[10];
   T2 = Time[10];
   
   ObjectCreate("label", OBJ_TREND, 0, T1, P1, T2, P2);
   ObjectSet("label", OBJPROP_RAY, 0);
   ObjectSet("label", OBJPROP_COLOR, Gold);
   ObjectSet("label", OBJPROP_STYLE, STYLE_SOLID);
   */

   
   
   
   
   
  
   
   
   
   
   
   }    
   
//---- Bollinger Bands------------------------------------------------- 
   for(i=0; i<Bars; i++)        
   {   
      
   UpperBuffer[i] = (1+Deviation/100)*iMAOnArray(MovingBuffer,0,BandsPeriod,MA_Shift,MethodMA,i);
   LowerBuffer[i] = (1-Deviation/100)*iMAOnArray(MovingBuffer,0,BandsPeriod,MA_Shift,MethodMA,i);
   MovingBufferMovingAverage[i] =     iMAOnArray(MovingBuffer,0,BandsPeriod,MA_Shift,MethodMA,i);    
 
   
   //MovingBufferMovingAverage[i] = iMAOnArray(MovingBuffer,0,BandsPeriod,0,0,i);    
   /*
   LowerBuffer[i] =               iBandsOnArray(MovingBuffer,0,BandsPeriod,BandsDeviations,MA_Shift,MODE_LOWER,i);
   UpperBuffer[i] =               iBandsOnArray(MovingBuffer,0,BandsPeriod,BandsDeviations,MA_Shift,MODE_UPPER,i); 
   MovingBufferMovingAverage[i] = iBandsOnArray(MovingBuffer,0,BandsPeriod,BandsDeviations,MA_Shift,MODE_MAIN,i);    
   */
//----Comment seront HAUT Gauche --------------------------------------
   /*
   double FreeMargin=AccountFreeMargin();
   Comment(""
   ,"\n",      "Account free margin: ",DoubleToStr(FreeMargin,2) 
   ,"\n",      ""
   ,"\n",      "Current time : ",TimeToStr(TimeCurrent()) 
   ,"\n",      ""
   ,"\n",      "Current time is of System     ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)
   ,"\n",      ""
   ,"\n",      "How are you Brother ",MovingBufferMovingAverage[i]
   ,"\n",      ""
   ,"\n",      "Current time is of System: ",TimeToStr(TimeCurrent(),TIME_SECONDS));
   */
   /*
   int   currentPeak=3.2;
   int   lastPeak=2.5;  
   DrawIndicatorTrendLine(Time[i], Time[i-1],MovingBufferMovingAverage[currentPeak],
         MovingBufferMovingAverage[lastPeak], Red, STYLE_DOT);
   */
  
//---------------------------------------------------------------------    
   /*
   Print("Account equity = ",AccountEquity());
   Print("Account free margin is ETSTTETTST ", AccountFreeMargin());
   */  
//---------------------------------------------------------------------      
   //----Incorrect-------------------
   ObjectCreate("Bar1", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet("Bar1", OBJPROP_CORNER, 4);
   ObjectSet("Bar1", OBJPROP_XDISTANCE, 20);
   ObjectSet("Bar1", OBJPROP_YDISTANCE,30); 
   ObjectSetText("Bar1", "Philippe Dadjo = "+iClose(Synbol,0,i), 10, "Arial Black", Green);
   //----Incorrect-------------------            
   }  
   
   
   datetime    T1,  
               T2;
   double      P1,
               P2; 
   double      Ecart=0.0020;                     
   P1 = MovingBufferMovingAverage[0]+Ecart; 
   T1 = Time[0];  
   P2 = MovingBufferMovingAverage[5];   
   if (ObjectFind("label") == 1)
   {
       ObjectSet("label", OBJPROP_PRICE1, P1);
       ObjectSet("label", OBJPROP_PRICE2, P1);
       ObjectSet("label", OBJPROP_TIME1, Time[0]);
       ObjectSet("label", OBJPROP_TIME2, Time[4]);
   }
   else
   {
       ObjectCreate("label", OBJ_TREND, 1, Time[0], P1, Time[4], P1);   
       ObjectSet("label", OBJPROP_RAY, 0);
       ObjectSet("label", OBJPROP_COLOR, Gold);
       ObjectSet("label", OBJPROP_STYLE, STYLE_DOT);  
   }
   
   datetime    Tone, 
               Ttwo;
   double      Pone,
               Ptwo;  
   Pone = MovingBufferMovingAverage[0]-Ecart;    
   Tone = Time[0];  
   Ptwo = MovingBufferMovingAverage[5];
   Ttwo = Time[5];      
   if (ObjectFind("label2") == 1)
   {
       ObjectSet("label2", OBJPROP_PRICE1, Pone);
       ObjectSet("label2", OBJPROP_PRICE2, Pone);
       ObjectSet("label2", OBJPROP_TIME1, Time[0]);
       ObjectSet("label2", OBJPROP_TIME2, Time[4]);
   }
   else
   {
       ObjectCreate("label2", OBJ_TREND, 1, Time[0], Pone, Time[4], Pone);  
       ObjectSet("label2", OBJPROP_RAY, 0);
       ObjectSet("label2", OBJPROP_COLOR, Gold);
       ObjectSet("label2", OBJPROP_STYLE, STYLE_DOT);  
   }
          
   return(0); 
   }  
//=================After Being Initialized==============================
   void CreateInfoObjects()
   {       
   ObjectCreate("BarClosing", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet("BarClosing", OBJPROP_CORNER, 1);
   ObjectSet("BarClosing", OBJPROP_XDISTANCE, 20);
   ObjectSet("BarClosing", OBJPROP_YDISTANCE,30); 
   ObjectSetText("BarClosing", "Philippe Dadjo YES YES YES "+Symbol()+"  ", 10, "Arial Black", Green);  


   ObjectCreate ("Correlation",OBJ_LABEL,0,0,MovingBufferMovingAverage[0]); 
   ObjectSet    ("Correlation", OBJPROP_CORNER,2);   
   ObjectSet    ("Correlation",OBJPROP_XDISTANCE,3);
   ObjectSet    ("Correlation",OBJPROP_YDISTANCE,12);  
   ObjectSetText("Correlation","Spread: "+iTime(NULL,0,0),12,"Tahoma",Red);    

   } 
//=====================================================================
   
   /*
   DrawIndicatorTrendLine(Time[100], 
                                 Time[50], 
                                 BandsDeviations,
                                 BandsDeviations, 
                                 Green, STYLE_SOLID);
   
   
   
   
   void DrawIndicatorTrendLine(datetime x1, datetime x2, double y1, 
                            double y2, color lineColor, double style)
   {
   int indicatorWindow = WindowFind(short_name);
   if(indicatorWindow < 0)
       return;
   string label = "MACD_DivergenceLine " + DoubleToStr(x1, 0);
   //ObjectDelete(label);
   ObjectCreate(label, OBJ_TREND, indicatorWindow, x1, y1, x2, y2, 
                0, 0);
   ObjectSet(label, OBJPROP_RAY, 0);
   ObjectSet(label, OBJPROP_COLOR, lineColor);
   ObjectSet(label, OBJPROP_STYLE, style);
   }
   */                                  
//=====================================================================
/*
    Trend_Line(Time[10],Time[0],Open[10],Open[0],Gold,STYLE_SOLID);
    void Trend_Line(
    datetime x1, datetime x2, double y1, 
    double y2, color lineColor, double style)
    {
    //~~~~~~~~~~
    string label = "_Trend_Line_";
    ObjectDelete(label);
    ObjectCreate(label, OBJ_TREND, 1, x1, y1, x2, y2, 0, 0);
    ObjectSet(label, OBJPROP_RAY, 0);
    ObjectSet(label, OBJPROP_COLOR, lineColor);
    ObjectSet(label, OBJPROP_STYLE, style);
    //~~~~~~~~~~
    }
*/   